﻿#!/usr/bin/env python
# -*- coding:utf-8 -*-

'thread introduction'

__author__='zhaoxp'

import time,sys,Queue
from multiprocessing.managers import BaseManager

class QueueManager(BaseManager):
	pass
	
# 这是分布式进程实验的客户端
if __name__=='__main__':
	QueueManager.register('get_task_queue')
	QueueManager.register('get_result_queue')
	
	# 连接到服务器，也就是运行taskmanager.py的机器:
	server_addr='127.0.0.1'
	print('Connect to server %s .' % server_addr)
	# 端口和验证码注意保持与taskmanager.py设置的完全一致:
	m=QueueManager(address=(server_addr,5001),authkey='abc')
	m.connect()
	
	# 获取Queue的对象:
	task=m.get_task_queue()
	result=m.get_result_queue()
	
	# 从task队列取任务,并把结果写入result队列:
	for i in range(10):
		try:
			n=task.get(timeout=1)
			print('run task %d * %d' % (n,n))
			r='%d * %d = %d' % (n,n,n*n)
			time.sleep(1)
			result.put(r)
		except Queue.Empty:
			print('task queue is empty')
		
	print('worker exit')